package com.edgardeng.generator.service;

import com.edgardeng.generator.entity.ColumnEntity;
import com.edgardeng.generator.entity.SQLDataSource;
import com.edgardeng.generator.entity.Table;
import com.edgardeng.generator.mapper.GeneratorMapper;
import com.edgardeng.utils.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 */
@Service
public class SysGeneratorService {
	@Autowired
	private GeneratorMapper sysGeneratorDao;

	@Autowired
	private DataProviderService dataProviderService;


	public List queryTables(SQLDataSource source) {
		return dataProviderService.queryTables(source);
	}

	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		return sysGeneratorDao.queryList(map);
	}

//	public int queryTotal(Map<String, Object> map) {
//		return sysGeneratorDao.queryTotal(map);
//	}

	public Table queryTable(String tableName) {
		Map<String, String> map = sysGeneratorDao.queryTable(tableName);
		return new Table(map.get("table_name"), map.get("table_comment"), map.get("create_time"));
	}

	public List<ColumnEntity> queryColumns(String tableName) {
		List<Map<String, Object>> maps =  sysGeneratorDao.queryColumns(tableName);
		List<ColumnEntity> columsList = new ArrayList<>();
		for(Map<String, Object> column : maps) {
			ColumnEntity columnEntity = new ColumnEntity();
			columnEntity.setName(column.get("name").toString());
			columnEntity.setType(column.get("type").toString());

			columnEntity.setRemark(column.get("remark").toString());
			columnEntity.setExtra(column.get("extra").toString());
			columnEntity.setIsKey(Integer.valueOf(column.get("isKey").toString())== 1);
			columsList.add(columnEntity);
		}
		return columsList;
	}

	public byte[] generatorCode(SQLDataSource model) {
		String[] tableNames = model.getTables().split(",");

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for(String tableName : tableNames){
			Table table = dataProviderService.queryATables(model, tableName); // 查询表信息
			List<ColumnEntity> columns = dataProviderService.queryTableColumns(model, tableName); // 查询列信息
			GenUtils.freemarkerGenCode(table, columns, zip, model); // 生成代码
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();

	}


   /**  生成代码 */
	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for(String tableName : tableNames){
			//查询表信息
			Table table = queryTable(tableName);
			//查询列信息
			List<ColumnEntity> columns = queryColumns(tableName);
			//生成代码
			// GenUtils.generatorCode(table, columns, zip);
			GenUtils.freemarkerGenCode(table,columns,zip, null);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	/**  生成数据表文档 md文件 */
	public byte[] generatorDBDocument( ) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<Map<String, Object>> tableList = queryList(new HashMap<String, Object>());

		int i = 1;
		for(Map<String, Object> table :tableList) {
			String tableName = table.get("tableName").toString();
			//查询列信息
			List<ColumnEntity> columns = queryColumns(tableName);
			table.put("columns", columns);
			table.put("index", String.valueOf(i));
			i++;
		}
		GenUtils.freemarkerGenDBDoc(tableList, zip);




		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

}
